<?php

namespace app\admin\model;


/**
 * 会员
 * Class InformationClassification
 * @package app\admin\model
 */
class User extends Base
{
    protected $autoWriteTimestamp = true;//开启自动添加时间戳
    protected $updateTime = false;

    //最后登录时间
    public function getLastTimeAttr($value){
        return !empty($value) ? date("Y-m-d H:i:s",$value) : "未激活";
    }

    //会员列表
    public function userList($keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("tel","like","%".$keyword."%")->whereOr("nick_name","like","%".$keyword."%");
                }
            })
            ->where(function ($q){
                if(!empty($this->substations)){//是否设置了当前网站
                    $q->where("substation_id",$this->substations);
                }else{
                    if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
                        $arr = [];
                        $Substation = new Substation();//分站类
                        $userList = $Substation->userList();//获取分站集合
                        foreach ($userList as $val){
                            $arr[] = $val['substation_id'];
                        }
                        $q->where("substation_id","in",$arr);
                    }
                }
            })
            ->order("create_time DESC")
            ->page($this->after_page)
            ->limit($this->after_limit)
            ->select();
    }

    //会员列表总数
    public function userListCount($keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    $q->whereOr("tel","like","%".$keyword."%")->whereOr("nick_name","like","%".$keyword."%");
                }
            })
            ->where(function ($q){
                if(!empty($this->substations)){//是否设置了当前网站
                    $q->where("substation_id",$this->substations);
                }else{
                    if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
                        $arr = [];
                        $Substation = new Substation();//分站类
                        $userList = $Substation->userList();//获取分站集合
                        foreach ($userList as $val){
                            $arr[] = $val['substation_id'];
                        }
                        $q->where("substation_id","in",$arr);
                    }
                }
            })
            ->count();
    }
}